package com.red.wood.module.task;

import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.red.wood.common.springjdbc.CommonDao;
import com.red.wood.common.util.CommonUtils;
import com.red.wood.common.util.ProvinceParse;

@Service
public class CdnProvinceParseService {

	private static final Log log = LogFactory.getLog(CdnProvinceParseService.class);
	
	@Autowired
	private CommonDao dao;
	
	/**
	 * CDN企业根据企业注册地找到对应所属省份
	 */
	public void cdnDataParse() {
		try {
			log.info("=============CDN企业信息插入开始==============");
			StringBuilder sqlSb = new StringBuilder("select uuid,cdnid,cdnname,cdnadd,corp,commandid,provinceid,timstamp,filecreatetime from t_dwd_cdn_cdndata where provinceid is null");
			List<Map<String, Object>> list = dao.queryTableData(null, sqlSb);
			if(list != null && list.size() > 0) {
				for(Map<String,Object> map : list) {
					String cdnAdd = CommonUtils.valueOf(map.get("CDNADD"));
					String province = ProvinceParse.addressAdscription(cdnAdd);
					map.put("PROVINCEID",CommonUtils.getProvinceId(province));
				}
			}
			dao.deleteTableData("delete from t_dwd_cdn_cdndata where provinceid is null");
			String sql = "insert into t_dwd_cdn_cdndata(uuid,cdnid,cdnname,cdnadd,corp,commandid,provinceid,timstamp,filecreatetime) values (?,?,?,?,?,?,?,?,?)";
			String columns = "uuid,cdnid,cdnname,cdnadd,corp,commandid,provinceid,timstamp,filecreatetime";
			dao.insertBatchTableData(list, sql, columns,10000);
			log.info("=============CDN企业信息插入结束==============");
		}catch(Exception e) {
			log.error("=============CDN企业信息插入异常==============");
			if(log.isErrorEnabled()) {
				log.error(e.getMessage(),e);
			}
		}
	}
	
	/**
	 * CDN客户根据地址找到对应所属省份
	 */
	public void cdnCustomerParse() {
		try {
			log.info("=============CDN客户信息插入开始==============");
			StringBuilder sqlSb = new StringBuilder("select uuid,cdnid,customeruuid,customerid,unitname,unitnature,idtype,idnumber,address,provinceid,timstamp,filecreatetime from t_dwd_cdn_customerinfo where provinceid is null and address is not null");
			List<Map<String, Object>> list = dao.queryTableData(null, sqlSb);
			
			for(Map<String,Object> map : list) {
				String address = CommonUtils.valueOf(map.get("ADDRESS"));
				
				String province = ProvinceParse.addressAdscription(address);
				map.put("PROVINCEID",CommonUtils.getProvinceId(province));
			}
			
			dao.deleteTableData("delete from t_dwd_cdn_customerinfo where provinceid is null and address is not null");
			String sql = "insert into t_dwd_cdn_customerinfo(uuid,cdnid,customeruuid,customerid,unitname,unitnature,idtype,idnumber,address,provinceid,timstamp,filecreatetime) values (?,?,?,?,?,?,?,?,?,?,?,?)";
			String columns = "uuid,cdnid,customeruuid,customerid,unitname,unitnature,idtype,idnumber,address,provinceid,timstamp,filecreatetime";
			dao.insertBatchTableData(list, sql, columns,10000);
			log.info("=============CDN客户信息插入结束==============");
		}catch(Exception e) {
			log.info("=============CDN企业信息插入异常==============");
			if(log.isErrorEnabled()) {
				log.error(e.getMessage(),e);
			}
		}
	}
}
